import requests
import pprint
import csv

f = open('全球疫情数据.csv', mode='a', encoding='utf-8', newline='')  # newline 不换行
csv_writer = csv.DictWriter(f, fieldnames=[
    'name','confirm','confirmAdd','heal','dead','nowConfirm'
])
csv_writer.writeheader()    # 写入头

url = 'https://api.inews.qq.com/newsqa/v1/automation/modules/list?modules=FAutoCountryConfirmAdd,WomWorld,WomAboard'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}

resp = requests.post(url=url, headers=headers)
json_data = resp.json()
# pprint.pprint(resp.json())
WomAboard = json_data['data']['WomAboard']
# print(WomAboard)

for wom in WomAboard:
    # name = wom['name']      # 国家名称
    # confirm = wom['confirm']    # 确诊人数
    # confirmAdd = wom['confirmAdd']  # 新增确诊人数
    # heal = wom['heal']      # 治愈人数
    # dead = wom['dead']      # 死亡人数
    # nowConfirm = wom['nowConfirm']      # 现有确诊人数
    # print(name,confirm,confirmAdd,heal,dead,nowConfirm)
    dic = {
        'name' : wom['name'],      # 国家名称
        'confirm' : wom['confirm'],    # 确诊人数
        'confirmAdd' : wom['confirmAdd'],  # 新增确诊人数
        'heal' : wom['heal'],      # 治愈人数
        'dead' : wom['dead'],      # 死亡人数
        'nowConfirm' : wom['nowConfirm']      # 现有确诊人数
    }

    # print(name,confirm,confirmAdd,heal,dead,nowConfirm)

    # 保存数据
    csv_writer.writerow(dic)
f.close()


